package com.jmail.sample;

import javax.mail.MessagingException;
import javax.mail.internet.AddressException;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import com.jmail.model.MailSender;
import com.jmail.model.entity.User;
import com.swtdesigner.SWTResourceManager;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;

public class MailComposer extends Shell {

	private Text txtFrom;
	private Text txtTo;
	private Text txtCC;
	private Text txtBCC;
	private Text txtSubject;
	private StyledText stxtContent;
	
	private User loginedUser = null;
	
	public User getLoginedUser() {
		return loginedUser;
	}

	public void setLoginedUser(User loginedUser) {
		this.loginedUser = loginedUser;
	}

	/**
	 * Launch the application.
	 * @param args
	 */
	public static void main(String args[]) {
		try {
			Display display = Display.getDefault();
			MailComposer shell = new MailComposer(display);
			shell.open();
			shell.layout();
			while (!shell.isDisposed()) {
				if (!display.readAndDispatch()) {
					display.sleep();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the shell.
	 * @param display
	 */
	public MailComposer(Display display) {
		super(display, SWT.SHELL_TRIM);
		createContents();
	}

	/**
	 * Create contents of the shell.
	 */
	protected void createContents() {
		setText("Mail Editor");
		setSize(750, 600);
		setText(getText());
		
		Label lblFrom = new Label(this, SWT.NONE);
		lblFrom.setFont(SWTResourceManager.getFont("Tahoma", 8, SWT.BOLD));
		lblFrom.setBounds(10, 15, 61, 14);
		lblFrom.setText("From:");
		
		txtFrom = new Text(this, SWT.BORDER);
		txtFrom.setEditable(false);
		txtFrom.setBounds(77, 10, 657, 19);
		
		txtTo = new Text(this, SWT.BORDER);
		txtTo.setText("phhuan504@yahoo.com");
		txtTo.setBounds(77, 35, 657, 19);
		
		Label lblTo = new Label(this, SWT.NONE);
		lblTo.setText("To:");
		lblTo.setFont(SWTResourceManager.getFont("Tahoma", 8, SWT.BOLD));
		lblTo.setBounds(10, 40, 61, 14);
		
		txtCC = new Text(this, SWT.BORDER | SWT.WRAP | SWT.MULTI);
		txtCC.setBounds(77, 60, 657, 38);
		
		Label lblCc = new Label(this, SWT.NONE);
		lblCc.setText("CC:");
		lblCc.setFont(SWTResourceManager.getFont("Tahoma", 8, SWT.BOLD));
		lblCc.setBounds(10, 65, 61, 14);
		
		txtBCC = new Text(this, SWT.BORDER | SWT.WRAP | SWT.MULTI);
		txtBCC.setBounds(77, 104, 657, 38);
		
		Label lblBcc = new Label(this, SWT.NONE);
		lblBcc.setText("BCC:");
		lblBcc.setFont(SWTResourceManager.getFont("Tahoma", 8, SWT.BOLD));
		lblBcc.setBounds(10, 109, 61, 14);
		
		txtSubject = new Text(this, SWT.BORDER);
		txtSubject.setText("Testing");
		txtSubject.setBounds(77, 184, 657, 19);
		
		Label lblSubject = new Label(this, SWT.NONE);
		lblSubject.setText("Subject:");
		lblSubject.setFont(SWTResourceManager.getFont("Tahoma", 8, SWT.BOLD));
		lblSubject.setBounds(10, 189, 61, 14);
		
		Label lblText = new Label(this, SWT.NONE);
		lblText.setText("Text:");
		lblText.setFont(SWTResourceManager.getFont("Tahoma", 8, SWT.BOLD));
		lblText.setBounds(10, 214, 61, 14);
		
		stxtContent = new StyledText(this, SWT.BORDER | SWT.FULL_SELECTION | SWT.WRAP);
		stxtContent.setText("Hello JMail");
		stxtContent.setBounds(77, 214, 657, 317);
		
		Button btnSend = new Button(this, SWT.NONE);
		btnSend.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				send();
			}
		});
		btnSend.setBounds(77, 537, 78, 23);
		btnSend.setText("Send");
		
		Button btnDiscard = new Button(this, SWT.NONE);
		btnDiscard.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				discard();
			}
		});
		btnDiscard.setText("Discard");
		btnDiscard.setBounds(161, 537, 78, 23);
	}

	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}

	private void send() {
		int iA = -1;
		int iDot = -1;
		String strFrom = "";
		
		//Build mail From address
		if (this.loginedUser.getHost().startsWith("mail.")) {
			strFrom = this.loginedUser.getUsername() + "@" + this.loginedUser.getHost().replaceFirst("mail.", "");
		} else {
			strFrom = this.loginedUser.getUsername() + "@" + this.loginedUser.getHost();
		}
		
		//Check e-mail address format of TO address
		iA = txtTo.getText().indexOf('@');
		iDot = txtTo.getText().lastIndexOf('.');
		
		if (!(iA >= 0 && iDot >= 0 && iDot > iA)) {
			MessageBox mBox = new MessageBox(this, SWT.ICON_INFORMATION | SWT.OK);
			mBox.setText("Information");
			mBox.setMessage("E-Mail address is incorrect!");
			mBox.open();
			this.txtTo.setFocus();
		}
		
		MailSender sender = new MailSender(strFrom, this.loginedUser.getUsername(), this.loginedUser.getPassword(), this.txtTo.getText(), this.loginedUser.getHost());
		sender.setStrSubject(this.txtSubject.getText());
		sender.setStrContent(this.stxtContent.getText());
		try {
			if (sender.send()) {
				System.out.println("Sent mail!");
			} else {
				System.out.println("Send mail failed!");
			}
		} catch (AddressException e) {
			e.printStackTrace();
			System.out.println("Send mail failed!");
		} catch (MessagingException e) {
			e.printStackTrace();
			System.out.println("Send mail failed!");
		}
	}

	private void discard() {
		dispose();
	}
}
